<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 表格区域 -->
    <el-card shadow="never" border="false">
      <!-- 表格头部 -->
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
          @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
      </template>
      <!-- 表格主体 -->
      <el-table
      :data="articleList"
      style="width: 100%">
      <el-table-column prop="stem"  label="标题"  width="400"></el-table-column>
      <el-table-column  prop="creator"  label="作者"  width="120"></el-table-column>
      <el-table-column  prop="likeCount"  label="点赞"  width="100"></el-table-column>
      <el-table-column  prop="views"  label="查看"  width="100"></el-table-column>
      <el-table-column  prop="createdAt"  label="创建时间"  width="200"></el-table-column>
      <el-table-column  prop=""  label="操作"  width="200">
        <!-- obj是变量名，可以随便取 -->
        <!-- 通过作用域插槽,提供了$index(数据下标)和row(行的数据) -->
        <template #default="obj">
          <div class="actions">
            <i class="el-icon-view" @click="openDrawer('preview', obj.row.id)"></i>
            <i class="el-icon-edit-outline" @click="openDrawer('edit', obj.row.id)"></i>
            <el-popconfirm
              title="确认删除？"
              @confirm="delArticle(obj.row.id)">
              <template #reference>
                <i class="el-icon-delete actions"></i>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @prev-click="handleCurrentChange"
      @next-click="handleCurrentChange"
      :current-page="current"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background>
    </el-pagination>
    </el-card>

    <!-- 抽屉区域 -->
    <!-- .sync 一种修饰符，添加之后，子组件内部也可以关闭抽屉 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="handleClose"
      size="60%">
<!-- 预览 -->
      <div v-if="drawerType === 'preview'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>
<!-- 创建、编辑 -->
      <el-form v-else ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input placeholder="请输入标题" v-model="form.stem">
          </el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>
        </el-form-item>
        <el-form-item v-if="drawerType === 'add' || drawerType === 'edit'">
          <el-button type="primary" @click="submit">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { addArticle, delArticle, getArticle, getArticleList, updateArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',
  data () {
    return {
      articleList: [],
      current: 1,
      pageSize: 10,
      total: 0,
      drawer: false,
      drawerType: 'add',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created () {
    this.initData()
  },
  components: {
    quillEditor
  },
  computed: {
    drawerTitle () {
      let title = '标题'
      if (this.drawerType === 'add') {
        title = '添加面经'
      } else if (this.drawerType === 'preview') {
        title = '查看面经'
      } else if (this.drawerType === 'edit') {
        title = '编辑面经'
      }
      return title
    }
  },
  methods: {
    async initData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      this.articleList = res.data.rows
      this.total = res.data.total
    },
    async delArticle (id) {
      await delArticle(id)
      this.$message.success('删除成功')
      if (this.articleList.length === 1 && this.current > 1) {
        this.current -= 1
      }
      this.initData()
    },
    handleSizeChange (val) {
      this.pageSize = val
      this.initData()
    },
    handleCurrentChange (val) {
      this.current = val
      this.initData()
    },
    // 打开抽屉
    async openDrawer (type, val) {
      this.drawerType = type
      this.drawer = true
      if (type === 'edit' || type === 'preview') {
        const res = await getArticle(val)
        this.form.stem = res.data.stem
        this.form.content = res.data.content
        this.form.id = val
      }
    },
    // 添加和编辑面经时，关闭要二次确认，查看时不用
    handleClose () {
      if (this.drawerType === 'add' || this.drawerType === 'edit') {
        this.$confirm('确认关闭？')
          .then(() => {
            this.closeDrawer()
          })
          .catch(() => {})
      } else {
        this.closeDrawer()
      }
    },
    // 添加面经
    async submit () {
      if (this.drawerType === 'add') {
        this.add()
      } else if (this.drawerType === 'edit') {
        this.update()
      }
    },
    // 添加面经
    async add () {
      // 1. 校验
      await this.$refs.form.validate()
      // 2. 请求
      await addArticle(this.form)
      // 3. 提示
      this.$message.success('面经创建成功')
      // 4. 关闭抽屉
      this.closeDrawer()
      // 5. 重新渲染
      this.current = 1
      this.initData()
    },
    // 修改面经
    async update () {
      // 1. 校验
      await this.$refs.form.validate()
      // 2. 请求
      await updateArticle(this.form)
      // 3. 提示
      this.$message.success('面经保存成功')
      // 4. 关闭抽屉
      this.closeDrawer()
      // 5. 重新渲染
      this.initData()
    },
    // 关闭抽屉方法
    closeDrawer () {
      if (this.drawerType === 'add' || this.drawerType === 'edit') {
        // 重置表单
        this.$refs.form.resetFields()
      }

      // 关闭抽屉
      this.drawer = false
    }
  }
}

</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
     i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
